/**********************************************************************
 Name:  CreateOrderNO
 Purpose: 生成销售订单或退货订单编号.
                格式:经销商代码 + 订单类型 + 日期（6位） + 流水号（3位）                                                      
-------                                                             
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR               DATE                 DETAIL                                
   1.0 -      huang xiaojiang      07/14/2011      Create          : 
***********************************************************************/

trigger CreateOrderNO on Order__c (after insert) {
    
    //订单类型标识
    string strSalesOrderKey='经销商订单';
    string strReturnOrderKey='经销商退订';
    //经销商id
    set<id> setDistriId=new set<id>();
    list<Order__c> lOrderHistory=new list<Order__c>();
    list<Order__c> lOrderUpdate=new list<Order__c>();
    //订单计数器
    map<id,integer> mapSalesOrderExists=new map<id,integer>();
    map<id,integer> mapReturnOrderExists=new map<id,integer>();
    map<id,integer> mapSalesOrderCur=new map<id,integer>();
    map<id,integer> mapReturnOrderCur=new map<id,integer>();
    
    for(Order__c o:trigger.new){
        if(o.OrderType__c==strSalesOrderKey||o.OrderType__c==strReturnOrderKey){
            setDistriId.add(o.Distributor_Name__c);    
        }
    }
    
    //查询今天已经存在的“经销商销售订单”或者“经销商退货订单”
    lOrderHistory=[select id,OrderType__c,Distributor_Name__c from Order__c where id not in:trigger.newMap.keySet() and Distributor_Name__c in:setDistriId and Distributor_Name__c!=null
        and (OrderType__c=:strSalesOrderKey or OrderType__c=:strReturnOrderKey) and Order_date__c=:date.today()];
        
    if(lOrderHistory.size()>0){
        for(Order__c o:lOrderHistory){
            if(o.OrderType__c==strSalesOrderKey){
                if(mapSalesOrderExists.get(o.Distributor_Name__c)==null){
                    mapSalesOrderExists.put(o.Distributor_Name__c,1);
                }else{
                    mapSalesOrderExists.put(o.Distributor_Name__c,mapSalesOrderExists.get(o.Distributor_Name__c)+1);    
                }
                
            }else{
                if(o.OrderType__c==strReturnOrderKey){
                    if(mapReturnOrderExists.get(o.Distributor_Name__c)==null){
                        mapReturnOrderExists.put(o.Distributor_Name__c,1);    
                    }else{
                        mapReturnOrderExists.put(o.Distributor_Name__c,mapReturnOrderExists.get(o.Distributor_Name__c)+1);      
                    }
                }                
            }
        }
    }
    
    //生成经销商订单编号
    for(Order__c o:trigger.new){
        if(o.OrderType__c==strSalesOrderKey){
        
            if(mapSalesOrderCur.get(o.Distributor_Name__c)==null){
                mapSalesOrderCur.put(o.Distributor_Name__c,1);    
            }else{
                mapSalesOrderCur.put(o.Distributor_Name__c,mapSalesOrderCur.get(o.Distributor_Name__c)+1);      
            }
           o.DistriOrderNo__c=o.DistributorCode__c+'-'+o.OrderTypeCode__c+'-'+dateTime.now().format('yyMMdd')
               +string.valueOf(mapSalesOrderExists.get(o.Distributor_Name__c)+mapSalesOrderCur.get(o.Distributor_Name__c));
               
           lOrderUpdate.add(o);
            
        }else{
        
            if(o.OrderType__c==strReturnOrderKey){
                if(mapReturnOrderCur.get(o.Distributor_Name__c)==null){
                    mapReturnOrderCur.put(o.Distributor_Name__c,1);    
                }else{
                    mapReturnOrderCur.put(o.Distributor_Name__c,mapReturnOrderCur.get(o.Distributor_Name__c)+1);      
                }
                o.DistriOrderNo__c=o.DistributorCode__c+'-'+o.OrderTypeCode__c+'-'+dateTime.now().format('yyMMdd')
                   +string.valueOf(mapReturnOrderExists.get(o.Distributor_Name__c)+mapReturnOrderCur.get(o.Distributor_Name__c));
                lOrderUpdate.add(o);               
            }                
        }    
    }    
        
    //更新经销商订单
    if(lOrderUpdate.size()>0){
        update lOrderUpdate;
    }
}